package ru.isys.xml.util.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import ru.isys.xml.XmlItem;
import ru.isys.xml.template.Bind;
import ru.isys.xml.template.BindEntyty;

public class BindBuilder
{
    private List <BindEntyty> bindEntities;
    private List<XmlItem> xmlItems;
    private String xml;
        
    public BindBuilder (List<XmlItem> xmlItems){
        this.xmlItems=xmlItems;
        this.bindEntities=new ArrayList<BindEntyty>();
        this.xml="";
    }
    
    public String build ()
    {
        prepareBindEntities();
        xml+=Bind.TAG;
        for (BindEntyty et:bindEntities){
            xml+=et.getTag();
        }
        xml+=Bind.CLOSE_TAG;
        return xml;
    }
    
    
	private void prepareBindEntities() {
		for (XmlItem item : xmlItems) {
			if (item.isModelField()) {
				if (item.getAttrs().isEmpty()) {
					bindEntities.add(new BindEntyty(item.getFullName(), item
							.getType(), item.isRequired(), item.isReadOnly()));
				} else {
					HashMap<String, String> attrs = item.getAttrs();
					for (Entry<String, String> set : attrs.entrySet()) {
						bindEntities.add(new BindEntyty(item.getFullName()
								+ "@" + set.getKey(), item.getType(), item
								.isRequired(), item.isReadOnly()));
					}
				}
			}
		}
    }
}
